<template>
  <a-card
    :bordered="false"
    class="card-area"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <h2>请选择运费模板</h2>
    <!-- 表格区域 -->
    <a-table ref="TableInfo"
             :columns="columns"
             :dataSource="dataSource"
             :pagination="pagination"
             :loading="loading"
             :defaultExpandAllRows="true"
             :scroll="{ x: 800 }"
             :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
             @change="handleTableChange">
      <template slot="operation" slot-scope="text, record">
        <a-button @click="deliverOrder(record)" type="primary" :loading="loading">发货</a-button>
      </template>
    </a-table>
    <div class="drawer-bootom-button">
      <div style="width: 50%">
        <a-button @click="deliverOrder()" type="primary" :loading="loading">发货</a-button>
        <a-button @click="reset()" :loading="loading">返回</a-button>
      </div>
    </div>
    <face-order
      ref="faceOrder"
      @close="handleFaceOrderClose"
      :faceOrderVisiable = "faceOrderVisiable">
    </face-order>
  </a-card>
</template>
<script>
import FaceOrder from './components/FaceOrder'
export default {
  components: { FaceOrder },
  name: 'DeliverOrder',
  data () {
    return {
      must: {
        mcb00: '', // 获取查询运费模板ID
        mba00: '' // 获取查询订单信息ID
      },
      advanced: false,
      dataSource: [],
      selectedRowKeys: [],
      faceOrderVisiable: false,
      checkedKeys: [],
      queryParams: {},
      sortedInfo: null,
      paginationInfo: null,
      pagination: {
        pageSizeOptions: ['10', '20', '30', '40', '100'],
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
      },
      loading: false,
      mbaFile: {},
      mbbFileList: [],
      sender: {}
    }
  },
  computed: {
    columns () {
      let {sortedInfo} = this
      sortedInfo = sortedInfo || {}
      return [{
        title: '模板名称',
        dataIndex: 'mcb01'
      }, {
        title: '计费类型',
        dataIndex: 'mcb02',
        customRender: (text, row, index) => {
          switch (text) {
            case 0:
              return '按重量'
            case 1:
              return '按体积'
            default:
              return text
          }
        }
      }, {
        title: '首量(KG)',
        dataIndex: 'mcb03'
      }, {
        title: '首费',
        dataIndex: 'mcb04'
      }, {
        title: '次费',
        dataIndex: 'mcb05'
      }, {
        title: '首体积(L)',
        dataIndex: 'mcb06'
      }, {
        title: '首费',
        dataIndex: 'mcb07'
      }, {
        title: '次费',
        dataIndex: 'mcb08'
      }, {
        title: '物流名称',
        dataIndex: 'mca01'
      }]
    }
  },
  mounted () {
    this.fetch()
  },
  methods: {
    onSelectChange (selectedRowKeys) {
      // console.log(selectedRowKeys)
      this.selectedRowKeys = selectedRowKeys
      for (let key in this.selectedRowKeys) {
        this.must.mcb00 = this.dataSource[selectedRowKeys[key]].mcb00 // 获取收货信息ID
        console.log(this.dataSource[this.selectedRowKeys[key]].mcb00)
      }
      this.must.mba00 = this.$route.query.mba00
    },
    handleTableChange (pagination, filters, sorter) {
      const pager = { ...this.pagination }
      pager.current = pagination.current
      this.pagination = pager
      this.paginationInfo = pagination
      this.filteredInfo = filters
      this.sortedInfo = sorter
      this.fetch({
        results: pagination.pageSize,
        page: pagination.current,
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...this.queryParams,
        ...filters
      })
    },
    handleFaceOrderSuccess () {
      this.faceOrderVisiable = false
    },
    handleFaceOrderClose () {
      this.faceOrderVisiable = false
    },
    reset () {
      this.loading = false
      this.$router.push({path: `./unshipped`})
    },
    deliverOrder () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择您需要的运费模板')
        return
      }
      if (this.selectedRowKeys.length > 1) {
        this.$message.warning('请最多选择一个运费模板')
        return
      }
      this.$refs.faceOrder.setAllValues(this.must) // 所有信息
      this.faceOrderVisiable = true
    },
    fetch (params = {}) {
      this.loading = true
      if (this.paginationInfo) {
        // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
        this.$refs.TableInfo.pagination.current = this.paginationInfo.current
        this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
        params.pageSize = this.paginationInfo.pageSize
        console.log(params.pageSize)
        // 下一页
        params.pageNum = this.paginationInfo.current
        console.log(params.pageNum)
      } else {
        // 如果分页信息为空，则设置为默认值
        params.pageSize = this.pagination.defaultPageSize
        params.pageNum = this.pagination.defaultCurrent
      }
      this.$api.freight.mcbDetail({
        ...params
      }).then((r) => {
        console.log(r)
        console.info('運費快遞詳情頁：', r)
        let data = r.data.data
        this.dataSource = data.records
        const pagination = { ...this.pagination }
        console.log(pagination)
        pagination.total = data.total
        this.pagination = pagination
        // 数据加载完毕，关闭loading
        this.loading = false
      })
    }
  }
}
</script>
